package com.chendechao.model_home.ui.recommend


import android.content.Intent
import androidx.fragment.app.viewModels
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.chendechao.lib_base.BaseFragment
import com.chendechao.model_home.databinding.FragmentRecommendBinding
import com.chendechao.model_home.model.RecommendMutilVideo
import dagger.hilt.android.AndroidEntryPoint

@AndroidEntryPoint
class RecommendFragment : BaseFragment<FragmentRecommendBinding,RecommendViewModel>(FragmentRecommendBinding::inflate),
    OnItemClickListener {
    override val viewModel: RecommendViewModel by viewModels()
    private lateinit var recyclerView: RecyclerView
    private lateinit var adapter: RecommendAdapter // 假设有一个 RecommendAdapter
    override fun initView() {


        viewModel.sendEvent(RecommendIntent.LoadData)

        recyclerView = binding.recommendVideoRecyclerView
        recyclerView.layoutManager = GridLayoutManager(requireContext(), 2)
        adapter =RecommendAdapter(this) //初始化适配器
        recyclerView.adapter=adapter
    }

    override fun initData() {
        collectState(viewModel.state){state ->
            updateUI(state)
        }

    }

    private fun updateUI(state:RecommendUIState){
//        Logger.d(state.recommendMutilVideo)
        adapter.submitList(state.recommendMutilVideo) //假设适配器有submitList方法

    }

    override fun onItemClick(item: RecommendMutilVideo) {
        val intent = Intent(requireContext(), RecommendActivity::class.java).apply {
            putExtra("video_data", item)
        }
        startActivity(intent)
    }


}